//
//  RBGoodsDetailBannerView.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/23.
//

#import "RBGoodsDetailBannerView.h"
#import "RBBannerView.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import "UIImageView+RBAdd.h"
#import <UIView+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBDefine.h"
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import <AVKit/AVKit.h>

@interface RBGoodsDetailBannerView()<RBBannerViewDelegate>

@property(nonatomic, strong) RBBannerView *bannerView;

@property(nonatomic, strong)UIView *indicatorView;
@property(nonatomic, assign)NSInteger totalPages;
@property(nonatomic, assign)NSInteger currentPage;

@end

@implementation RBGoodsDetailBannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
    }
    return self;
}

#pragma mark - RBBannerViewDelegate

- (NSInteger)numberOfItemsInBannerView:(RBBannerView *)bannerView
{
    return self.dataSource.count;
}

- (void)bannerView:(RBBannerView *)bannerView willSetupView:(UIView *)view withIndex:(NSInteger)index
{
    if ([view isKindOfClass:[UIImageView class]])
    {
        UIImageView *imgView = (UIImageView *)view;
        
        UIView *playView = [imgView viewWithTag:333];
        if (!playView)
        {
            playView = UIView.new;
            playView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            playView.tag = 333;
            [playView rbSetCornerRadius:Zoom(35)];
            [imgView addSubview:playView];
            [playView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(0);
                make.width.height.mas_equalTo(Zoom(70));
            }];
            [playView view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:@"play"] withHeight:CGFLOAT_MIN width:Zoom(30) top:0 bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        }
        
        id data = self.dataSource[index];
        if ([data isKindOfClass:[UIImage class]])
        {
            imgView.image = (UIImage *)data;
        }
        else if ([data isKindOfClass:[NSString class]])
        {
            if ([data hasSuffix:@"mp4"]) {
                playView.hidden = NO;
                [imgView setImageWithUrl:self.videoFirst placeHolder:nil];
            } else {
                playView.hidden = YES;
                [imgView setImageWithUrl:(NSString *)data placeHolder:nil];
            }
        }
    }
}

- (void)bannerView:(RBBannerView *)bannerView scrollToIndex:(CGFloat)index
{
    self.currentPage = (NSInteger)roundf(index);
}

- (void)bannerView:(RBBannerView *)bannerView didSelectView:(UIView *)view withIndex:(NSInteger)index
{
    id data = self.dataSource[index];
    if ([data isKindOfClass:[NSString class]]) {
        if ([data hasSuffix:@"mp4"]) {
            AVPlayerViewController *vc = AVPlayerViewController.new;
            NSString *path = data;
            vc.player = [AVPlayer playerWithURL:[NSURL URLWithString:path]];
            [vc.player play];
            [self.viewController presentViewController:vc animated:YES completion:nil];
            
            return;
        }
    }
    if (self.selectBlock)
    {
        self.selectBlock(index);
    }
}

#pragma mark - Setter

- (void)setDataSource:(NSArray *)dataSource
{
    _dataSource = dataSource;
    [self.bannerView reloadData];
    self.indicatorView.hidden = dataSource.count <= 1;
    self.totalPages = dataSource.count;
}

- (void)setTotalPages:(NSInteger)totalPages
{
    _totalPages = totalPages;
    [self.indicatorView removeAllSubviews];
    UIView *leftView = nil;
    for (NSInteger i = 0; i < totalPages; i++)
    {
        UIView *itemView = UIView.new;
        itemView.tag = i;
        [itemView rbSetCornerRadius:3.5];
        itemView.backgroundColor = UIColor.whiteColor;
        [self.indicatorView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(0);
            if (leftView)
            {
                make.left.mas_equalTo(leftView.mas_right).offset(7);
            }
            else
            {
                make.left.mas_equalTo(0);
            }
            if (i == totalPages - 1)
            {
                make.right.mas_equalTo(0);
            }
        }];
        leftView = itemView;
    }
    self.currentPage = 0;
}

- (void)setCurrentPage:(NSInteger)currentPage
{
    _currentPage = currentPage;
    for (UIView *itemView in self.indicatorView.subviews)
    {
        [itemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(itemView.tag == currentPage? 22: 7);
        }];
    }
}

#pragma mark - Getter

- (RBBannerView *)bannerView
{
    if (!_bannerView)
    {
        self.bannerView = ({
            RBBannerView *view = [RBBannerView new];
            view.deleagte = self;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _bannerView;
}

- (UIView *)indicatorView
{
    if (!_indicatorView)
    {
        self.indicatorView = ({
            UIView *view = UIView.new;
            view.userInteractionEnabled = NO;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(-11);
                make.height.mas_equalTo(7);
                make.centerX.mas_equalTo(0);
            }];
            view;
        });
    }
    return _indicatorView;
}

@end
